var $table = $('#table');

var $caddbtn = $("#cadd");
var $queryBtn = $('#queryBtn');
var $cedit = $('#cedit');
var $cdel = $('#cdel');
var $start = $('#start');
var $stop = $('#stop');


$(function () {

    $("#attrib_02").change(function(){

        var eimtabname =$(this).children('option:selected').val();
        if(eimtabname==''){
            $('#attrib_19').prop("disabled",true);
        }else{
            $('#attrib_19').prop("disabled",false);
        }
        rapid.post({
            url: "/sysadmin/merge/getEimAttrib",
            data: "eimtabname=" + eimtabname,
            success: function (data) {


                $('#attrib_19').empty();


                for (var a=0;a<data.length;a++) {
                    var dataContent = data[a];

                    var code3 =  "<option value='"+dataContent.column_name+"'>" + dataContent.column_name + "</option>";
                    $('#attrib_19').append(code3);
                }

                $('#attrib_19').selectpicker('refresh');
            }
        });

    });



    $("#attrib_01").change(function(){

        var modetabname =$(this).children('option:selected').val();

        if(modetabname==''){
            $('#attrib_20').prop("disabled",true);
        }else{
            if(modetabname=='FN_FORM_DATA'){
                $('#attrib_05').prop("disabled",false);


            }else{
                $('#attrib_05').prop("disabled",true);
                $('#attrib_05').val("");
            }
            $('#attrib_20').prop("disabled",false);
        }
        rapid.post({
            url: "/sysadmin/merge/getModeUkey",
            data: "modetabname=" + modetabname,
            success: function (data) {


                $('#attrib_20').empty();
                $('#attrib_20').append("<option value=''>请选择主数据模型主键</option>");

                for (var a=0;a<data.length;a++) {
                    var dataContent = data[a];

                    var code3 =  "<option value='"+dataContent.value+"'>" + dataContent.text +"--"+dataContent.value +"</option>";
                    $('#attrib_20').append(code3);
                }


            }
        });

    });

    $('.form-control-chosen').chosen({
        allow_single_deselect: true,
        width: '100%'
    });


    $cedit.click(function () {

        var info = $table.bootstrapTable('getSelections');

        if (info.length != 1) {
            rapid.info("请选择一行编辑!");
        } else {
            rapid.post({
                url: "/sysadmin/merge/getMerge",
                data: "cid=" + info[0].conflict_id,
                success: function (result) {

                    if(result.attrib_02==''){
                        $('#attrib_19').prop("disabled",true);
                    }else{
                        $('#attrib_19').prop("disabled",false);

                        rapid.post({
                            url: "/sysadmin/merge/getEimAttrib",
                            data: "eimtabname=" + result.attrib_02,
                            success: function (data) {


                                $('#attrib_19').empty();


                                for (var a=0;a<data.length;a++) {
                                    var dataContent = data[a];

                                    var code3 =  "<option value='"+dataContent.column_name+"' >" + dataContent.column_name + "</option>";

                                    $('#attrib_19').append(code3);
                                }

                                if(result.attrib_19!=null &&　result.attrib_19!='') {
                                    var arr = result.attrib_19.split(',');
                                    $('#attrib_19').selectpicker('val', arr);
                                }
                                $('#attrib_19').selectpicker('refresh');

                            }
                        });
                    }


                    if(result.attrib_01==''){
                        $('#attrib_20').prop("disabled",true);
                    }else{
                        $('#attrib_20').prop("disabled",false);
                   
                        if(result.attrib_01=='FN_FORM_DATA') {
                            $('#attrib_05').prop("disabled",false);
                            $("#attrib_05 option[value='" + result.attrib_05 + "']").prop("selected", true);
                        }else{
                            $("#attrib_05 option[value='']").prop("selected", true);
                        }
                        rapid.post({
                            url: "/sysadmin/merge/getModeUkey",
                            data: "modetabname=" +  result.attrib_01,
                            success: function (data) {


                                $('#attrib_20').empty();

                                $('#attrib_20').append("<option value=''>请选择主数据模型主键</option>");
                                for (var a=0;a<data.length;a++) {
                                    var dataContent = data[a];
                                    if(result.attrib_20==dataContent.value) {
                                        var code3 = "<option value='" + dataContent.value + "' selected='selected'>" + dataContent.text + "--" + dataContent.value + "</option>";
                                    }else{
                                        var code3 = "<option value='" + dataContent.value + "' >" + dataContent.text + "--" + dataContent.value + "</option>";
                                    }
                                    $('#attrib_20').append(code3);
                                }



                            }
                        });
                    }

                    $("#myModal").modal();
                    $("#conflict_id").val(result.conflict_id);
                    $("#cid").val(result.row_id);
                    $("#attrib_12").val(result.attrib_12);
                   // $("#attrib_11").val(result.attrib_11);



                    $("#attrib_02 option[value='" + result.attrib_02 + "']").prop("selected", true);
                    $("#attrib_01 option[value='" + result.attrib_01 + "']").prop("selected", true);
                    $("#attrib_04 option[value='" + result.attrib_04 + "']").prop("selected", true);
                }
            });
        }
    });


    $cdel.click(function () {
        var info = $table.bootstrapTable('getSelections');
        var ids = "";
        for (let i = 0; i < info.length; i++) {
            ids += info[i].conflict_id + ",";

        }

        if (info.length < 1) {
            rapid.info("请至少选择一行删除!");
        } else {
            rapid.post({
                url: "/sysadmin/merge/deleteMerges",
                data: "ids=" + ids,
                success: function (result) {

                    loadTalbe($table)
                }
            });
        }

    });


    $start.click(function () {
        var info = $table.bootstrapTable('getSelections');
        var ids = "";
        for (let i = 0; i < info.length; i++) {
            ids += info[i].conflict_id + ",";

        }

        if (info.length < 1) {
            rapid.info("请至少选择一行启动!");
        } else {
            rapid.post({
                url: "/sysadmin/merge/mergeStart",
                data: "ids=" + ids,
                success: function (result) {
                    loadTalbe($table)
                }
            });
        }
    });


    $stop.click(function () {
        var info = $table.bootstrapTable('getSelections');
        var ids = "";
        for (let i = 0; i < info.length; i++) {
            ids += info[i].conflict_id + ",";

        }
        if (info.length < 1) {
            rapid.info("请至少选择一行停止!");
        } else {
            rapid.post({
                url: "/sysadmin/merge/mergeStop",
                data: "ids=" + ids,
                success: function (result) {
                    loadTalbe($table)
                }
            });
        }
    });

    $caddbtn.click(function () {

        $("#myModal").modal();

    });

    function loadTalbe(table){
        table.bootstrapTable('destroy');  // 销毁原表格
        inittab();
        var searchvaleimtablename = $("#searchString_eimtablename").val();
        var searchvalmodelname= $("#searchString_modelname").val();
        var searchvalgxgz=$("#searchString_gxgz").val();
        var searchvalstate=$("#searchString_state").val();
        table.bootstrapTable('refresh', {
            query: {
                eimtablename: searchvaleimtablename,
                modelname: searchvalmodelname,
                gxgz:searchvalgxgz,
                state:searchvalstate,
                pageIndex: 1,
                pageSize:10
            }
        });
    }

    $queryBtn.click(function () {

        loadTalbe($table)
    });


    $('#myModal').on('hidden.bs.modal', function () {
        $("#mergeform").data('bootstrapValidator').destroy();
        $('#mergeform').data('bootstrapValidator', null);

        $("#conflict_id").val("");
        $('#attrib_19').selectpicker('val',[]);
        $("#attrib_20 option").remove();


        $('#attrib_19').prop("disabled",true);
        $('#attrib_20').prop("disabled",true);
        $('#attrib_19').selectpicker('refresh');
        formValidator($("#mergeform"));
        $("#mergeform")[0].reset();
    })




    function changeDateFormat(cellval) {
        var val=cellval+"";

        if( val != '' && val !=null && val !='null' &&  val!='undefined')
        {
            return val.substring(0,10)
        }

    }

    function formValidator(fm) {
        fm.bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                /*输入框不同状态，显示图片的样式*/
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                /*验证*/
                attrib_01: {
                    /*键名username和input name值对应*/
                    validators: {
                        notEmpty: {
                            /*非空提示*/
                            message: '请选择主数据模型名称'
                        },     callback: {
                            message: '请选择动态表单',
                            callback: function(value, validator) {
                                var zsj=$("#attrib_05").val();

                                validator.updateStatus('attrib_05', 'VALID');
                                if(zsj=="" && value=="FN_FORM_DATA" ){
                                    return false;
                                }else {
                                    return true;
                                }
                            }
                        }
//                           stringLength: {/*长度提示*/
//                               min: 6,
//                               max: 30,
//                               message: '用户名在6到30之间'
//                           }/*最后一个没有逗号*/
                    }
                },
                attrib_02: {
                    validators: {
                        notEmpty: {
                            message: '请选择EIM表名'
                        },
                    }
                },attrib_04: {
                    validators: {
                        notEmpty: {
                            message: '请选择归并规则'
                        },
                        callback: {
                            message: 'Sources规则请选择EIM字段',
                            callback: function(value, validator) {
                                var zdm=$("#attrib_19").val();
                                validator.updateStatus('attrib_19', 'VALID');
                                if(value=="EIMS" && zdm.length<=0){
                                    return false;
                                }else {
                                    return true;
                                }
                            }
                        }
                    }
                },attrib_12: {
                    validators: {
                        notEmpty: {
                            message: '请填写导入任务定时规则'
                        },
                    }
                },attrib_20: {
                    validators: {
                        notEmpty: {
                            message: '请选择主数据模型主键'
                        },
                    }
                },attrib_05: {
                    validators: {

                        callback: {
                            message: '请选择动态表单',
                            callback: function(value, validator) {
                                var zsj=$("#attrib_01").val();
                                validator.updateStatus('attrib_01', 'VALID');
                                if(value=="" && zsj=="FN_FORM_DATA" ){
                                    return false;
                                }else {
                                    return true;
                                }
                            }
                        }
                    }
                }/*,attrib_11: {
                    validators: {
                        notEmpty: {
                            message: '请填写优先级'
                        },
                    }
                }*/,attrib_19: {
                    validators: {
                        callback: {
                            message: 'Sources规则请选择EIM字段',
                            callback: function(value, validator) {
                                var gz=$("#attrib_04").val();
                                validator.updateStatus('attrib_04', 'VALID');
                                if(gz=="EIMS" && value.length<=0){
                                    return false;
                                }else {
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
        })


    }


    $("#sava-edit-btn").click(function () {
        $("#mergeform").bootstrapValidator('validate');
        var flag = $("#mergeform").data('bootstrapValidator').isValid();
        if (flag) {

            rapid.confirm("确定要保存吗？", function (yes) {
                if (yes) {
                    rapid.post({
                        url: "/sysadmin/merge/saveMerge",
                        data: $("#mergeform").serialize()+"&eimzdm="+$("#attrib_19").val(),
                        success: function (result) {
                            loadTalbe($table);
                            $('#myModal').modal('hide');
                        },fail: function(rs, msg) {

                        }
                    });
                }
            });
        }


    })

    function inittab() {
        $table.bootstrapTable({
            url: "/sysadmin/merge/getMerges",
            idField: 'conflict_id',
            dataType: 'json',
            sidePagination: "server", //服务端处理分页
            queryParamsType: "undefined",//设置为 ‘limit’ 则会发送符合 RESTFul 格式的参数.
            queryParams: function (params) {//自定义参数，这里的参数是传给后台的，我这是是分页用的


                return {//这里的params是table提供的
                    pageIndex: params.pageNumber,//从数据库第几条记录开始
                    pageSize: params.pageSize,//找多少条
                    eimtablename: $("#searchString_eimtablename").val(),
                    modelname: $("#searchString_modelname").val(),
                    gxgz: $("#searchString_gxgz").val(),
                    state:$("#searchString_state").val()
                }

            },
            responseHandler: function (res) {
                var result = {total: 0, rows: []};

                if (res.recordCount > 0) {
                    result.total = res.recordCount;
                    result.rows = res.list;
                }
                return result;
            },
            sortable: true,

            pagination: 'true',  //开启分页
            paginationPreText: '上一页',
            paginationNextText: '下一页',
            // singleSelect: true, //单选

            showColumns: true,
            clickToSelect: true,
            showRefresh: true,
            columns: [
                {
                    field: 'check', checkbox: true, formatter: function (value, row, index) {

                        if (row.check == true) {

                            return {checked: true};
                        }
                    }
                },
                {
                    field: 'created', title: '创建日期', class: 'colStyle'
                }, {
                    field: 'created_by_name', title: '创建人', class: 'colStyle'
                },
                {field: 'attrib_01_cn', title: '主数据模型名称', width: 300, class: 'colStyle'},
                {field: 'attrib_20', title: '主数据模型主键', width: 300, class: 'colStyle'},
                {field: 'attrib_02', title: 'EIM表名', width: 300, class: 'colStyle'},
                {field: 'attrib_05_cn', title: '动态表单名', width: 300, class: 'colStyle'},
                {field: 'attrib_19', title: 'EIM字段名', class: 'colStyle'},
               // {field: 'attrib_11', title: '导入优先级', class: 'colStyle'},
                {field: 'attrib_04_cn', title: '归并规则', class: 'colStyle'},
                {field: 'attrib_12', title: '导入任务定时规则', class: 'colStyle'},
                {field: 'attrib_15', title: '定时任务状态', class: 'colStyle'}
            ]


        });
    }
    $(document).ready(function(){
        formValidator($("#mergeform"));
        inittab();
    });

})
